﻿using Easy.Common.Helper.JWT;
using Easy.Common.Params;
using Easy.Common.Results;
using Easy.Services.DbContexts;
using Easy.Services.Dtos;
using Easy.Services.Entities;
using Easy.Services.Interface;
using System.Linq.Expressions;

namespace Easy.Services.Core
{
    internal class RoleService : IRoleService
    {


        public long Add(Role role)
        {
            using MySQLDbContext db = new();
            BaseService<Role> baseService = new(db);
            return baseService.AddBase(role);

        }

        public Role Get(Expression<Func<Role, bool>> expression)
        {
            using MySQLDbContext db = new();
            BaseService<Role> baseService = new(db);
            return baseService.GetBase(expression);

        }

        public RolePermissionDTO GetDTO(long id)
        {
            using MySQLDbContext db = new();
            var role = Get(r => r.Id == id);
            var permIds = db.RolePermissionRelations.Where(r => r.RoleId == id && r.IsDelete == false).Select(r => r.PermissionId).ToArray();
            RolePermissionDTO dto = new()
            {
                Description = role.Description,
                Id = id,
                Name = role.Name,
                PermIds = permIds,
                State = role.State
            };

            return dto;
        }

     
        public APIResult Update(Role role)
        {
            using MySQLDbContext db = new();
            BaseService<Role> baseService = new(db);
            APIResult result = new()
            {
                Message = "更新失败"
            };
            var model = Get(r => r.Id == role.Id);
            if (model == null)
            {
                result.Message = "角色不存在";
                return result;
            }
            role.CreaterDate = model.CreaterDate;
            role.CreaterId = model.CreaterId;
            role.CreaterName = model.CreaterName;
            var res = baseService.UpdateBase(role);
            if (res)
            {
                result.Success = res;
                result.Message = "更新成功";
            }
            return result;

        }

        public APIResult Delete(long id, TokenModelJwt tokenModel)
        {
            using MySQLDbContext db = new();
            BaseService<Role> baseService = new(db);
            APIResult result = new() { Message = "更新失败" };
            var role = Get(r => r.Id == id);
            if (role == null)
            {
                result.Message = "角色不存在";
                return result;
            }
            role.EditDate = DateTime.Now;
            role.EditId = tokenModel.Id;
            role.EditName = tokenModel.UserName;
            role.SetDelete();
            var res = baseService.UpdateBase(role);
            if (res)
            {
                result.Success = res;
                result.Message = "更新成功";
            }
            return result;

        }

        public bool Exist(Expression<Func<Role, bool>> expression)
        {
            using MySQLDbContext db = new();
            BaseService<Role> baseService = new(db);
            return baseService.Exist(expression);

        }

        public List<Role> GetList(Expression<Func<Role, bool>> expression = null)
        {
            using MySQLDbContext db = new();
            BaseService<Role> baseService = new(db);
            return baseService.GetList(expression);

        }


        public APIResult GetPage(PageParam param)
        {
            APIResult result = new();
            if (param == null || param.Page <= 0 || param.PageSize <= 0)
            {
                result.Message = "分页参数无效";
                return result;
            }
            PageResult<List<RoleDTO>> pageResult = new();
            using MySQLDbContext db = new();
            BaseService<Role> baseService = new(db);
            pageResult.Rows = baseService.GetPage(param).Select(role => new RoleDTO() { Id = role.Id, Description = role.Description, Name = role.Name, State = role.State }).ToList();
            pageResult.Total = baseService.GetTotalCount();
            result.Success = pageResult.Rows.Count > 0;
            result.Message = result.Success ? "获取成功" : "获取失败";
            result.Data = pageResult;
            return result;

        }


    }
}
